function fWriteAAMDataFile (AAMParams, dataFileName, dir, varargin)
    if ~isempty( varargin)
        if (strcmp(varargin(1), 'bmp'))
            ext = '.bmp';
        elseif strcmp(varargin(1), 'jpg')
             ext = '.jpg';
        else
            error ('Invalid extension');
        end
    else
        ext = '';
    end
      
    file_AAMDataFile = fopen([dir '//' dataFileName] ,'w');
    num_Images = size(AAMParams,1);
    AAM_dim = size(AAMParams,2);
    
    for i = 1:num_Images
        line = sprintf('%d ',AAM_dim);
        for j = 1:AAM_dim
            token = sprintf('%f ',AAMParams(i,j));
            line = [line token];
        end
        index = sprintf('%4.4d',i);
        fprintf(file_AAMDataFile, ['Recon-' index ext '\n']);
        
        if i < num_Images
            fprintf(file_AAMDataFile, [line '\n']);
        else
            fprintf(file_AAMDataFile, line);  
        end
    end
    fclose(file_AAMDataFile);
end